﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System.Text.RegularExpressions;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCB.UI.PopWindows
{
    public partial class S42AddYearPlanJJ : EFViewCmes
    {
        public S42AddYearPlanJJ(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, System.Windows.RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            bool _validateField = ThisValidateField() & this.ValidateField();//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            EIRow eiRow = this.grid.EIBlock.GetRow(0);
            if (ValidateMonth(eiRow))
            {
                return;
            }

            string fdFeb = (eiRow["fdFeb"] == null) ? "" : eiRow["fdFeb"].ToString();
            string fdJan = (eiRow["fdJan"] == null) ? "" : eiRow["fdJan"].ToString();
            string fdMar = (eiRow["fdMar"] == null) ? "" : eiRow["fdMar"].ToString();
            string fdApr = (eiRow["fdApr"] == null) ? "" : eiRow["fdApr"].ToString();
            string fdMay = (eiRow["fdMay"] == null) ? "" : eiRow["fdMay"].ToString();
            string fdJun = (eiRow["fdJun"] == null) ? "" : eiRow["fdJun"].ToString();
            string fdJul = (eiRow["fdJul"] == null) ? "" : eiRow["fdJul"].ToString();
            string fdAug = (eiRow["fdAug"] == null) ? "" : eiRow["fdAug"].ToString();
            string fdSep = (eiRow["fdSep"] == null) ? "" : eiRow["fdSep"].ToString();
            string fdOct = (eiRow["fdOct"] == null) ? "" : eiRow["fdOct"].ToString();
            string fdNov = (eiRow["fdNov"] == null) ? "" : eiRow["fdNov"].ToString();
            string fdDec = (eiRow["fdDec"] == null) ? "" : eiRow["fdDec"].ToString();

            ValidateDate(eiRow);

            string _actionMode = Mode.ToString() == null ? "insert" : Mode.ToString().ToLower();
            EIInfo info = new EIInfo();
            info.Set(EIConstants.ServiceNameKey, "ZMCB42");
            info.Set(EIConstants.MethodNameKey, _actionMode);
            info.SetBlock(this.EIDataContext.GetBlock("result"));
            this.Submit(info, DoCommitCompleted);
        }

        /// <summary>
        /// 使开始、结束日期与月明细对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ValidateDate(EIRow eiRow)
        {
            string[] month = new string[12] { "fdJan", "fdFeb", "fdMar", "fdApr", "fdMay", "fdJun", "fdJul", "fdAug", "fdSep", "fdOct", "fdNov", "fdDec" };
            String BeginDate = fdBeginDate.Value.ToString();
            String year = fdYear.Value.ToString();
            DateTime begin = DateTime.Parse(BeginDate);
            int beginMonth = begin.Month;
            String EndDate = fdEndDate.Value.ToString();
            DateTime end = DateTime.Parse(EndDate);
            int endMonth = end.Month;
            for (int i = 0; i < 12; i++)
            {
                if (!(eiRow[month[i]] == null))
                {
                    String beginDate = year + "/" + (i + 1) + "/1";
                    this.fdBeginDate.Value = beginDate;
                        break;
                }
            }
            for (int j = 11; j >= 0; j--)
            {
                if (!(eiRow[month[j]] == null))
                {
                    String day="";
                    switch (j + 1)
                    {
                        case 1: day = "/31"; break;
                        case 2: day = "/28"; break;
                        case 3: day = "/31"; break;
                        case 4: day = "/30"; break;
                        case 5: day = "/31"; break;
                        case 6: day = "/30"; break;
                        case 7: day = "/31"; break;
                        case 8: day = "/31"; break;
                        case 9: day = "/30"; break;
                        case 10: day = "/31"; break;
                        case 11: day = "/30"; break;
                        case 12: day = "/31"; break;
                    }

                    String endDate = year + "/" + (j + 1) + day;
                    this.fdEndDate.Value = endDate;
                        break;
                }
            }

        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 验证
        /// </summary>
        public bool ValidateMonth(EIRow eiRow)
        {
            string[] month = new string[12] { "fdJan", "fdFeb", "fdMar", "fdApr", "fdMay", "fdJun", "fdJul", "fdAug", "fdSep", "fdOct", "fdNov", "fdDec" };
            for (int i = 0; i < 12; i++)
            {
                if (!(eiRow[month[i]] == null))
                {

                    if (Regex.IsMatch(eiRow[month[i]].ToString(), @"((^\d{1,6}$)|(^\d{1,6}\.\d{0,4}$)|(^0\.\d{0,4}$))"))
                    {
                        
                    }
                    else
                    {
                        ValidateMessagePopup((i + 1).ToString() + "月的值只能输入整数位不超过6位，小数位不超过4位的数字");
                        this.TempButtonOpen(true);//确定按钮生效
                        return true;
                    }
                }
            }

            return false;

        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void fdRoadwayId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            EFComboBox box = field.Content as EFComboBox;
            var result = from c in box.Items where c.GetType().GetProperty("fdKeyId").GetValue(c, null).ToString().Equals(field.Value.ToString()) select c;

            if (result.Count() > 0)
            {
                object Item = result.First();
                this.fdLength.Value = Item.GetType().GetProperty("fdLength").GetValue(Item, null);
                this.fdSurplusLength.Value = Item.GetType().GetProperty("fdSurplusLength").GetValue(Item, null);
                this.fdSection.Value = Item.GetType().GetProperty("fdSection").GetValue(Item, null);
                this.fdRoadwayType.Value = Item.GetType().GetProperty("fdRoadwayType").GetValue(Item, null);
                this.fdMineRock.Value = Item.GetType().GetProperty("fdMineRock").GetValue(Item, null);
            }
        }

        private void fdPlan_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            double SurplusLength = 0.0, Plan = 0.0;
            if (fdSurplusLength.Value != null)
            {
                Double.TryParse(fdSurplusLength.Value.ToString(), out SurplusLength);
            }

            if (fdPlan.Value != null)
            {
                Double.TryParse(fdPlan.Value.ToString(), out Plan);
            }
            this.fdSurplus.Value = Math.Round(SurplusLength - Plan, 0);
        }
    }
}
